﻿using System;
using System.Collections.Generic;
using System.Data;
using BusinessObjects.Report;
using BusinessObjects.Report.Voucher;
using DataObjects.Shared;
using DataObjects.StandartInterfaces.Report;

namespace DataObjects.ADO.SqlServer.Report
{
    /// <summary>
    /// Defines methods to access Report List
    /// </summary>
    /// <remarks>
    /// This is a database-independent class. SqlServermplementations are database specific
    /// </remarks>
    public class SqlServerReportList : IReportListDao
    {

        #region IReportList Members

        /// <summary>
        /// Gets the report list.
        /// </summary>
        /// <param name="reportListId">The report list identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public ReportList GetReportList(long reportListId)
        {
            const string sql = @"uspGet_ReportList_ByID";
            object[] parms = { "@ReportListId", reportListId };
            return Db.Read(sql, CommandType.StoredProcedure, Make, parms);
        }

        public List<ReportList> GetPaged(int unitId, int currentPage)
        {
            const string sql = @"uspGetPaged_ReportList";
            object[] parms = { "@WhereClause", " WHERE UnitID = " + unitId, "@OrderBy", null, "@PageIndex", currentPage };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        public int GetPageSize(int unitId)
        {
            const string sql = @"uspGetPageSize_ReportList";
            object[] parms = { "@WhereClause", " WHERE UnitID = " + unitId };
            return Db.GetCount(sql, CommandType.StoredProcedure, parms);
        }

        /// <summary>
        /// Gets the report lists.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<ReportList> GetReportLists(int type)
        {
            const string sql = @"uspGetAll_ReportList";
            object[] parms = { "@Type", type };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }
        /// <summary>
        /// Gets the report lists.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<ReportList> GetReportLists()
        {
            const string sql = @"uspGetAll_ReportList";
            return Db.ReadList(sql, CommandType.StoredProcedure, Make);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="accountId"></param>
        /// <returns></returns>
        public List<ReportList> GetReportListsForComboTree(long accountId)
        {
            const string sql = @"uspGet_ReportList_ByUnit";
            object[] parms = { "@UnitID", 1 };
            return Db.ReadList(sql, CommandType.StoredProcedure, Make, parms);
        }

        /// <summary>
        /// Inserts the report list.
        /// </summary>
        /// <param name="reportList">The report list.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public string InsertReportList(ReportList reportList)
        {
            const string sql = @"uspInsert_ReportList";
            return Db.Insert(sql, Take(reportList));
            //reportList.Version = GetReportList(reportList.ReportListId).Version;
        }

        /// <summary>
        /// Updates the report list.
        /// </summary>
        /// <param name="reportList">The report list.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public string UpdateReportList(ReportList reportList)
        {
            const string sql = @"uspUpdate_ReportList";
            return Db.Update(sql, CommandType.StoredProcedure, Take(reportList));
        }

        /// <summary>
        /// Deletes the report list.
        /// </summary>
        /// <param name="reportList">The report list.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public string DeleteReportList(ReportList reportList)
        {
            const string sql = @"uspDelete_ReportList";
            return Db.Update(sql, CommandType.StoredProcedure, Take(reportList));
        }

        /// <summary>
        /// Creates a ReportList object based on DataReader.
        /// </summary>
        private static readonly Func<IDataReader, ReportList> Make = reader =>
           new ReportList
           {
               ReportListId = reader["ReportListID"].AsId(),
               Code = reader["Code"].AsString(),
               ReportGroupId = reader["ReportGroupID"].AsLong(),
               Name = reader["Name"].AsString(),
               ParentId = reader["ParentId"].AsInt(),
               File = reader["ReportFile"].AsString(),
               InputTypeName = reader["InputTypeName"].AsString(),
               FunctionReportName = reader["FunctionReportName"].AsString(),
               ProcedureReportName = reader["ProcedureReportName"].AsString(),
               TableName = reader["TableName"].AsString(),
               Grade = reader["Grade"].AsInt(),
               Description = reader["Description"].AsString(),
               SortOrder = reader["SortOrder"].AsInt(),
               IsTypical = reader["IsTypical"].AsBool(),
               IsActive = reader["IsActive"].AsBool(),
               ReportController = reader["ReportController"].AsString()


           };

        /// <summary>
        /// Creates query parameters list from ReportList object
        /// </summary>
        /// <param name="reportList"></param>
        /// <returns>Name value parameter list.</returns>
        private static object[] Take(ReportList reportList)
        {
            return new object[]  
            {
                "@ReportListId", reportList.ReportListId,
                "@ReportGroupId", reportList.ReportGroupId,
                "@Description", reportList.Description,
                "@File", reportList.File,
                "@FunctionReportName", reportList.FunctionReportName,
                "@Grade", reportList.Grade,
                "@InputTypeName", reportList.InputTypeName,
                "@Name", reportList.Name,
                "@IsTypical", reportList.IsTypical,
                "@IsActive", reportList.IsActive,
                "@ProcedureReportName", reportList.ProcedureReportName,
                "@SortOrder", reportList.SortOrder,
                "@TableName", reportList.TableName,
                "@ParentId",reportList.ParentId
            };
        }

        #endregion

        #region IStockBacklog Members
        /// <summary>
        /// Gets the report C22 h.
        /// </summary>
        /// <returns></returns>
        public List<StockBacklogEntity> GetReportStockBacklog(DateTime fromDate, DateTime toDate, string stockId)
        {
            const string sql = @"uspReport_Stock_Backlog";
            object[] parms = { "@FromDate", fromDate, "@ToDate",toDate, "@PStockID", stockId };
            return Db.ReadList(sql, CommandType.StoredProcedure, MakeStockBacklog, parms);
        }

        /// <summary>
        /// The make C22 hd
        /// </summary>
        private static readonly Func<IDataReader, StockBacklogEntity> MakeStockBacklog = reader =>
           new StockBacklogEntity
           {
               InventoryItemCode = reader["InventoryItemCode"].AsString(),
               InventoryItemName = reader["InventoryItemName"].AsString(),
               Unit = reader["Unit"].AsString(),
               QuantityOpening = reader["QuantityOpening"].AsInt(),
               QuantityInwardStock = reader["QuantityInwardStock"].AsInt(),
               QuantityOutwardStock = reader["QuantityOutwardStock"].AsInt(),
               QuantityClosing = reader["QuantityClosing"].AsInt(),
               OrgPriceOpening = reader["OrgPriceOpening"].AsDecimal(),
               OrgPriceInwardStock = reader["OrgPriceInwardStock"].AsDecimal(),
               OrgPriceOutwardStock = reader["OrgPriceOutwardStock"].AsDecimal(),
               OrgPriceClosing = reader["OrgPriceClosing"].AsDecimal(),
               FontStyle = reader["FontStyle"].AsString()
           };
        #endregion
    }
}
